package com.inossem.warehousestorage_inossem.activity.allocation.out_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.extra.AllocationOutStorageExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageResultActivity;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationOutMaterialsNoSearchAdapter;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutOverViewBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutSubmitBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutSubmitRequestBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationOutMaterialsRequest;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationOutStorageSearchMaterialsRequest;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationOutSubmitRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.AllocationUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;

/**
 * 物料列表 不帶搜索
 *
 * @author 詹建宇
 * @time on 2018/3/29 17:30
 * @email jianyu.zhan@inossem.com
 */
public class AllocationOutMaterialsNoSearchActivity extends BaseActivity implements BaseInteface {

    private TextView title, nextStep;
    private PullableListView listView;

    private List<ItemListBean> allData;//物料列表

    private String id, type;
    private AllocationOutMaterialsNoSearchAdapter adapter;

    private String stock_output_id;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_allocating_materials);

        initView(this);
        SPUtils.removeAllotionOutStorageInformation(this);
    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        listView = (PullableListView) activity.findViewById(R.id.listView);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);

        listView.setPullDownFlag(false);
        listView.setPullUpFlag(false);
    }

    @Override
    public void initData(Activity activity) {
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_in_storage_receive_list_title_short), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getConfirmDialog(activity, getResources().getString(R.string.activity_storage_manage_clear_prompt), new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        finish();
                    }
                }).show();
            }
        });
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (adapter.isOk()) {
                    submitSN(activity);
                } else {
                    Utils.middleToast(activity, getResources().getString(R.string.activity_in_storage_free_overview_at_least_one_item));
                }
            }
        });
        id = "";
        type = "";
        getExtra(activity);
        getData(activity);
    }

    private void getExtra(Activity activity) {
        id = activity.getIntent().getStringExtra(AllocationOutStorageExtra.ID);
        type = activity.getIntent().getStringExtra(AllocationOutStorageExtra.ALLOCATION_TYPE);
    }

    private void getData(Context context) {
        String pickingOutStorageInformation = SPUtils.getAllotionOutStorageInformation(context);
        if (TextUtils.isEmpty(pickingOutStorageInformation)) {
            switch (type) {
                case "0":
                    AllocationOutMaterialsRequest.request(context, id, new GsonBeanListener<AllocationOutOverViewBean>() {
                        @Override
                        public void onSuccess(AllocationOutOverViewBean bean) {
                            Gson gson = new Gson();
                            String json = gson.toJson(bean, AllocationOutOverViewBean.class);
                            SPUtils.saveAllocationOutStorageInformation(context, json);
                            allData = bean.getBody().getItem_list();
                            title.setText(bean.getBody().getRefer_receipt_code());
                            addListData(context, ActivityUtils.getShowData(allData, 0), listView, true);
                            SPUtils.saveAllocationOutStorageInformation(context, json);
                        }
                    });
                    break;
                case "1":
                    AllocationOutStorageSearchMaterialsRequest.request(context, null, null, null, id, new GsonBeanListener<AllocationOutOverViewBean>() {
                        @Override
                        public void onSuccess(AllocationOutOverViewBean bean) {
                            Gson gson = new Gson();
                            String json = gson.toJson(bean, AllocationOutOverViewBean.class);
                            SPUtils.saveAllocationOutStorageInformation(context, json);
                            allData = bean.getBody().getItem_list();
                            title.setText(bean.getBody().getRefer_receipt_code());
                            addListData(context, ActivityUtils.getShowData(allData, 0), listView, true);
                            SPUtils.saveAllocationOutStorageInformation(context, json);
                        }
                    });
                    break;
            }

        } else {
            Gson gson = new Gson();
            AllocationOutOverViewBean bean = gson.fromJson(pickingOutStorageInformation, AllocationOutOverViewBean.class);
            allData = bean.getBody().getItem_list();
            title.setText(bean.getBody().getRefer_receipt_code());
            addListData(context, ActivityUtils.getShowData(allData, 0), listView, true);
        }
    }

    private void addListData(Context context, List<ItemListBean> list, ListView listView, boolean flag) {
        adapter = new AllocationOutMaterialsNoSearchAdapter(context, list);
        listView.setAdapter(adapter);
    }

    private void submitSN(Activity activity) {

        AllocationOutSubmitRequestBean submitRequestBean = AllocationUtils.getAllocationOutSubmitRequestBean(activity, stock_output_id, "");

        AllocationOutSubmitRequest.request(activity, submitRequestBean, TextUtils.isEmpty(stock_output_id) ? "0" : "", new GsonBeanListener<AllocationOutSubmitBean>() {
            @Override
            public void onSuccess(AllocationOutSubmitBean bean) {
                LogUtils.i("onSuccess:" + bean.getBody());
                SPUtils.removePickingOutStorageInformation(activity);
                if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                    Utils.middleToast(activity, bean.getHead().getMsg());
                }
                Intent intent = new Intent(activity, OutStorageResultActivity.class);
                startActivity(intent);
            }

            @Override
            public void onFailing(AllocationOutSubmitBean bean) {
                super.onFailing(bean);
                stock_output_id = bean.getBody();
            }
        });
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(this, getResources().getString(R.string.activity_storage_manage_clear_prompt), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                finish();
            }
        }).show();
    }

}
